/*
 * Copyright (c) 2013-2015 Charkey. All rights reserved.
 *
 * This software is the confidential and proprietary information of Charkey.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with Charkey.
 *
 * Charkey MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * Charkey SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package cn.simastudio.talos.core.controller;

import cn.simastudio.talos.core.model.base.User;
import cn.simastudio.talos.core.service.base.UserService;
import cn.simastudio.talos.core.service.base.impl.PasswordService;
import cn.simastudio.talos.mybatis.pagehelper.PageHelper;
import cn.simastudio.talos.mybatis.pagehelper.PageInfo;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Usage:图书控制器类
 *
 * @author Labaijos
 * @date 2015/6/22 19:50
 */
@RestController
@RequestMapping("/console/users")
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    @Autowired
    PasswordService passwordService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public JSONObject getUserList(@RequestParam(value = "iDisplayStart", defaultValue = "0", required = false) int start,
                                  @RequestParam(value = "iDisplayLength", defaultValue = "10", required = false) int size,
                                  @RequestParam(value = "sSearch", defaultValue = "", required = false) String name) {
        User user = new User();
        if (!name.trim().equals("")) {
            user.setUsername(name.trim());
        }
        int page = start / size + 1;
        PageHelper.startPage(page, size);
        List<User> list = userService.getUserList(user);
        PageInfo pageInfo = new PageInfo(list);
        JSONObject data = new JSONObject();
        data.put("recordsTotal", pageInfo.getTotal());
        data.put("recordsFiltered", pageInfo.getTotal());
        list = list != null ? list : new ArrayList<User>();
        data.put("aaData", list);
        return data;
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public User addUser(@RequestBody User user) {
        user.setSalt(passwordService.getRandomSalt());
        user.setStatus(1);
        userService.create(user);
        return user;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User getUser(@PathVariable Integer id) {
        return userService.getUserRoleDetail(id);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public User updateUser(@PathVariable Integer id, @RequestBody User user) {
        user.setId(id);
        userService.updateUser(user);
        return user;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public User deleteUser(@PathVariable Integer id, @RequestBody User user) {
        user.setId(id);
        userService.updateUser(user);
        return user;
    }
}
